package com.cow.framework.utils.file;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.name.Rename;
/**
 * 图片处理工具
 * 
 * @author yangsj
 * */
public class ImageUtil {
	/**压缩图片*/
	public static void compressImage(File fromImage,File toImage,Integer toSize,Integer toWidth,Integer toHeight){
		Builder<File> builder = Thumbnails.of((File)fromImage);
				
		//压缩图片  
		try {
			BufferedImage bufferedImage = builder.asBufferedImage();
			if(toWidth != null && toHeight != null){
				builder.size(toWidth, toHeight);
			}else if(toWidth != null && bufferedImage.getWidth() > toWidth){
				builder = builder.width(toWidth);
			}else if(toHeight != null && bufferedImage.getHeight() > toHeight){
				builder = builder.width(toWidth);
			}
			
			builder.toFile(toImage); 
    		if(toImage.getTotalSpace() >= toSize * 1024){
    			builder.outputQuality((toSize * 1024)/toImage.getTotalSpace()).toFile(toImage); 
    		}
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	/**压缩图片*/
	public static void compressImage(String fromImage,String toImage,Integer toSize,Integer toWidth,Integer toHeight){
		Builder<File> builder = Thumbnails.of(fromImage);
		Builder<File> fromBuilder = Thumbnails.of(fromImage);
		boolean compressed = false;
				
		//压缩图片  
		try {
			BufferedImage bufferedImage = builder.scale(1f).asBufferedImage();
			
			if(toWidth != null && bufferedImage.getWidth() > toWidth && toHeight != null && bufferedImage.getHeight() > toHeight){
				fromBuilder = fromBuilder.size(toWidth, toHeight).keepAspectRatio(false);
				compressed = true;
			}else if(toWidth != null && bufferedImage.getWidth() > toWidth){
				fromBuilder = fromBuilder.width(toWidth);
				compressed = true;
			}else if(toHeight != null && bufferedImage.getHeight() > toHeight){
				fromBuilder = fromBuilder.width(toWidth);
				compressed = true;
			}
			if(compressed){
				fromBuilder.toFile(toImage); 				
			}else{
				FileUtil.copyFile(fromImage, toImage, true);
			}
			
			Builder<File> toBuilder = Thumbnails.of(toImage);
			File toFile = new File(toImage);
			if(toSize != null){
				if(toFile.length() >= toSize * 1024){
	    			toBuilder.outputQuality((toSize * 1024)/toFile.length()).toFile(toImage); 
	    		}
			}    		
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	/**压缩图片*/
	public static void compressImage(File fromImage,String toImage,Integer toSize,Integer toWidth,Integer toHeight){
		Builder<File> builder = Thumbnails.of(fromImage);
		Builder<File> fromBuilder = Thumbnails.of(fromImage);
				
		//压缩图片  
		try {
			BufferedImage bufferedImage = builder.scale(1f).asBufferedImage();
			if(toWidth != null && toHeight != null){
				fromBuilder = fromBuilder.size(toWidth, toHeight);
			}else if(toWidth != null && bufferedImage.getWidth() > toWidth){
				fromBuilder = fromBuilder.width(toWidth);
			}else if(toHeight != null && bufferedImage.getHeight() > toHeight){
				fromBuilder = fromBuilder.width(toWidth);
			}else{
				fromBuilder = fromBuilder.scale(1f);
			}
			
			fromBuilder.toFile(toImage); 
			Builder<File> toBuilder = Thumbnails.of(toImage);
			File toFile = new File(toImage);
			if(toSize != null){
				if(toFile.length() >= toSize * 1024){
	    			toBuilder.outputQuality((toSize * 1024)/toFile.length()).toFile(toImage); 
	    		}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	/**压缩图片
	 * @throws Exception */
	public static void compressImage(InputStream fromImage,String toImage,Integer toSize,Integer toWidth,Integer toHeight) throws Exception{
		BufferedImage bi=ImageIO.read(fromImage); 
				
		Builder builder = Thumbnails.of(bi);
		Builder fromBuilder = Thumbnails.of(bi);
				
		//压缩图片  
		try {
			BufferedImage bufferedImage = builder.scale(1f).asBufferedImage();
			if(toWidth != null && toHeight != null){
				fromBuilder = fromBuilder.size(toWidth, toHeight);
			}else if(toWidth != null && bufferedImage.getWidth() > toWidth){
				fromBuilder = fromBuilder.width(toWidth);
			}else if(toHeight != null && bufferedImage.getHeight() > toHeight){
				fromBuilder = fromBuilder.width(toWidth);
			}else{
				fromBuilder = fromBuilder.scale(1f);
			}
			
			fromBuilder.toFile(toImage); 
			Builder<File> toBuilder = Thumbnails.of(toImage);
			File toFile = new File(toImage);
			if(toSize != null){
				if(toFile.length() >= toSize * 1024){
	    			toBuilder.outputQuality((toSize * 1024)/toFile.length()).toFile(toImage); 
	    		}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	/**对目录生成缩略图*/
	public static void compressDest(String dest,Integer toWidth,Integer toHeight) throws IOException{
		Thumbnails.of(new File(dest).listFiles())
	    .size(toWidth, toHeight)
	    .outputFormat("png")
	    .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
	}
	  
	/**图片合并*/
    public static void mergeImage(File file1, File file2,String destFile) throws IOException {        
        BufferedImage image1 = ImageIO.read(file1);  
        BufferedImage image2 = ImageIO.read(file2);  
  
        BufferedImage combined = new BufferedImage(120, 245, BufferedImage.TYPE_INT_RGB);  
        
        // paint both images, preserving the alpha channels  
        Graphics2D g = (Graphics2D)combined.getGraphics();  
        g.setBackground(Color.gray);
        g.clearRect(0, 0, 120, 245);     
        g.setPaint(Color.RED);   
        
        g.drawImage(image1, 0, 0, null);  
        g.drawImage(image2, 0, 125, null);  
          
        // Save as new image  
        ImageIO.write(combined, "JPG", new File(destFile));  
    }  
    
    /**图片合并*/
    public static void mergeImage(File file1, File file2,File file3,String destFile) throws IOException {        
        BufferedImage image1 = ImageIO.read(file1);  
        BufferedImage image2 = ImageIO.read(file2); 
        BufferedImage image3 = ImageIO.read(file3); 
  
        BufferedImage combined = new BufferedImage(245, 245, BufferedImage.TYPE_INT_RGB);  
  
        // paint both images, preserving the alpha channels  
        Graphics2D g = (Graphics2D)combined.getGraphics();  
        g.setBackground(Color.gray);
        g.clearRect(0, 0, 245, 245);     
        g.setPaint(Color.RED);         
        
        g.drawImage(image1, 62, 0, null);  
        g.drawImage(image2, 0, 125, null);  
        g.drawImage(image3, 125, 125, null); 
          
        // Save as new image  
        ImageIO.write(combined, "JPG", new File(destFile));  
    }   
    
    /**图片合并*/
    public static void mergeImage(File file1, File file2,File file3,File file4,String destFile) throws IOException {
        BufferedImage image1 = ImageIO.read(file1);  
        BufferedImage image2 = ImageIO.read(file2); 
        BufferedImage image3 = ImageIO.read(file3);
        BufferedImage image4 = ImageIO.read(file4);
  
        BufferedImage combined = new BufferedImage(245, 245, BufferedImage.TYPE_INT_RGB);  
  
        // paint both images, preserving the alpha channels  
        Graphics2D g = (Graphics2D)combined.getGraphics();  
        g.setBackground(Color.gray);
        g.clearRect(0, 0, 245, 245);     
        g.setPaint(Color.RED); 
        
        g.drawImage(image1, 0, 0, null); 
        g.drawImage(image2, 125, 0, null); 
        g.drawImage(image3, 0, 125, null);  
        g.drawImage(image4, 125, 125, null);          
        
        // Save as new image  
        ImageIO.write(combined, "JPG", new File(destFile));  
    } 
    
    /**图片合并*/
    public static void mergeImage(List<File> fileList,String destFilePath) throws IOException { 
    	 int size = fileList.size();
    	 if(size == 1){
    		 FileUtil.copyFile(fileList.get(0).getPath(), destFilePath, true);
    	 }else if(size == 2){
    		 mergeImage(fileList.get(0), fileList.get(1),destFilePath);
    	 }else if(size == 3){
    		 mergeImage(fileList.get(0), fileList.get(1), fileList.get(2),destFilePath);
    	 }else if(size == 4){
    		 mergeImage(fileList.get(0), fileList.get(1), fileList.get(2), fileList.get(3),destFilePath);
    	 }
    } 
    
    /**图片合并*/
    public static void mergeImage(List<File> fileList,int width,int height,String destFilePath) throws IOException { 
    	BufferedImage combined = new BufferedImage(width * fileList.size(), height, BufferedImage.TYPE_INT_RGB); 
    	Graphics g = combined.getGraphics(); 
    	for(int i = 0;i < fileList.size();i++){
    		File file = fileList.get(i);
    		BufferedImage image = ImageIO.read(file); 
    		g.drawImage(image, i * width, 0, null); 
    	}
          
        // Save as new image  
        ImageIO.write(combined, "JPG", new File(destFilePath));  
    } 
}
